// ignore_for_file: prefer_const_constructors

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import "package:junyeyishu/utils/utils.dart";
import 'package:webview_flutter/webview_flutter.dart';

class QGVerifyAliPage extends StatefulWidget {
  final String phone;
  const QGVerifyAliPage({super.key, required this.phone});

  @override
  State<QGVerifyAliPage> createState() => _QGVerifyAliPageState();
}

class _QGVerifyAliPageState extends State<QGVerifyAliPage> {
  bool loading = true;

  final controller = WebViewController.fromPlatformCreationParams(
      PlatformWebViewControllerCreationParams());

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
      },
      child: Scaffold(
          backgroundColor: Color.fromRGBO(0, 0, 0, 0.3),
          body: Stack(
            children: [
              Opacity(
                opacity: loading ? 0.03 : 1.0,
                child: Center(
                  child: SizedBox(
                    width: 320.rpx,
                    height: 400.rpx,
                    child: buildWebView(),
                  ),
                ),
              ),
              loading
                  ? Center(
                      child: CircularProgressIndicator(color: Colors.white))
                  : Container(),
            ],
          )),
    );
  }

  buildWebView() {
    return InAppWebView(
        initialData: InAppWebViewInitialData(data: getAliVerifyHTML()),
        initialOptions: InAppWebViewGroupOptions(),
        // initialSettings: InAppWebViewSettings(),
        onLoadStop: (controller, url) {
          loading = false;
          setState(() {});
        },
        onWebViewCreated: (controller) {
          controller.addJavaScriptHandler(
              handlerName: 'verifyParam',
              callback: (args) {
                onSuccess(args[0]);
              });
          controller.addJavaScriptHandler(
              handlerName: 'bizResult',
              callback: (args) async {
                onError();
              });
        });
  }

  onSuccess(verifyParam) {
    var param = {};

    param["captchaVerifyParam"] = base64Encode(utf8.encode(verifyParam));
    param["phone"] = widget.phone;
Map<String, dynamic> stringMap = param.cast<String, dynamic>();

    Network.post(url_captchaToSms, data: stringMap).then((res) {
      if (!res.success) {
        toast(res.msg);
        Future.delayed(Duration(seconds: 1), () {
          Navigator.of(context).pop();
        });
        return;
      } else {
        toast(res.data["msg"]);
      }

      param["sign"] = res.data["sign"];

      Future.delayed(Duration(seconds: 1), () {
        Navigator.of(context).pop(param);
      });
    });
  }

  onError() async {
    await Future.delayed(const Duration(seconds: 1));
    Navigator.of(context).pop();
  }
}
